
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QGroupBox,
                             QLabel, QLineEdit, QPushButton, QTextEdit,
                             QGridLayout, QSizePolicy)
from PyQt5.QtCore import Qt


class UITabs:
    @staticmethod
    def create_basic_tab():
        widget = QWidget()
        main_layout = QHBoxLayout()
        left_layout = QVBoxLayout()
        right_layout = QVBoxLayout()

        # 左侧输入区域
        input_panel = QGroupBox("加解密参数")
        input_layout = QVBoxLayout()

        # 明文输入
        plaintext_section = QVBoxLayout()
        plaintext_label = QLabel("输入明文内容")
        plaintext_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        plaintext_edit = QLineEdit()
        plaintext_edit.setPlaceholderText("请输入16位二进制或十六进制数值")
        plaintext_edit.setMinimumHeight(35)
        plaintext_section.addWidget(plaintext_label)
        plaintext_section.addWidget(plaintext_edit)

        # 密钥输入
        key_section = QVBoxLayout()
        key_label = QLabel("输入加密密钥")
        key_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        key_edit = QLineEdit()
        key_edit.setPlaceholderText("请输入16位二进制或十六进制密钥")
        key_edit.setMinimumHeight(35)
        key_section.addWidget(key_label)
        key_section.addWidget(key_edit)

        input_layout.addLayout(plaintext_section)
        input_layout.addSpacing(15)
        input_layout.addLayout(key_section)
        input_panel.setLayout(input_layout)
        left_layout.addWidget(input_panel)

        # 操作按钮
        control_panel = QGroupBox("操作控制")
        control_layout = QVBoxLayout()
        encrypt_btn = QPushButton("执行加密操作")
        decrypt_btn = QPushButton("执行解密操作")
        encrypt_btn.setMinimumHeight(40)
        decrypt_btn.setMinimumHeight(40)
        encrypt_btn.setStyleSheet("QPushButton { background-color: #3498db; color: white; border-radius: 5px; }")
        decrypt_btn.setStyleSheet("QPushButton { background-color: #2ecc71; color: white; border-radius: 5px; }")
        control_layout.addWidget(encrypt_btn)
        control_layout.addSpacing(10)
        control_layout.addWidget(decrypt_btn)
        control_panel.setLayout(control_layout)
        left_layout.addWidget(control_panel)

        # 右侧输出区域
        output_panel = QGroupBox("处理结果")
        output_layout = QVBoxLayout()

        cipher_section = QVBoxLayout()
        cipher_label = QLabel("加密输出")
        cipher_label.setStyleSheet("font-weight: bold; color: #e74c3c;")
        ciphertext_output = QLineEdit()
        ciphertext_output.setReadOnly(True)
        ciphertext_output.setMinimumHeight(35)
        ciphertext_output.setStyleSheet("background-color: #f8f9fa; border: 1px solid #ddd;")
        cipher_section.addWidget(cipher_label)
        cipher_section.addWidget(ciphertext_output)

        decrypt_section = QVBoxLayout()
        decrypt_label = QLabel("解密输出")
        decrypt_label.setStyleSheet("font-weight: bold; color: #27ae60;")
        decrypt_output = QLineEdit()
        decrypt_output.setReadOnly(True)
        decrypt_output.setMinimumHeight(35)
        decrypt_output.setStyleSheet("background-color: #f8f9fa; border: 1px solid #ddd;")
        decrypt_section.addWidget(decrypt_label)
        decrypt_section.addWidget(decrypt_output)

        output_layout.addLayout(cipher_section)
        output_layout.addSpacing(15)
        output_layout.addLayout(decrypt_section)
        output_panel.setLayout(output_layout)
        right_layout.addWidget(output_panel)

        main_layout.addLayout(left_layout)
        main_layout.addSpacing(20)
        main_layout.addLayout(right_layout)
        widget.setLayout(main_layout)

        return widget, plaintext_edit, key_edit, encrypt_btn, decrypt_btn, ciphertext_output, decrypt_output

    @staticmethod
    def create_ascii_tab():
        widget = QWidget()
        layout = QHBoxLayout()
        left_panel = QVBoxLayout()
        right_panel = QVBoxLayout()

        # 左侧输入区域
        input_section = QGroupBox("ASCII模式参数")
        input_layout = QVBoxLayout()

        text_input_area = QVBoxLayout()
        text_label = QLabel("原始文本内容")
        text_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        ascii_input = QTextEdit()
        ascii_input.setMaximumHeight(100)
        ascii_input.setPlaceholderText("在此输入ASCII文本内容...")
        text_input_area.addWidget(text_label)
        text_input_area.addWidget(ascii_input)

        key_input_area = QVBoxLayout()
        key_label = QLabel("设置加密密钥")
        key_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        ascii_key = QLineEdit()
        ascii_key.setPlaceholderText("16位密钥")
        ascii_key.setMinimumHeight(35)
        key_input_area.addWidget(key_label)
        key_input_area.addWidget(ascii_key)

        input_layout.addLayout(text_input_area)
        input_layout.addSpacing(15)
        input_layout.addLayout(key_input_area)
        input_section.setLayout(input_layout)
        left_panel.addWidget(input_section)

        # 按钮区域
        button_section = QGroupBox("功能操作")
        button_layout = QVBoxLayout()
        ascii_encrypt_btn = QPushButton("ASCII文本加密")
        ascii_decrypt_btn = QPushButton("ASCII文本解密")
        ascii_encrypt_btn.setMinimumHeight(35)
        ascii_decrypt_btn.setMinimumHeight(35)
        button_layout.addWidget(ascii_encrypt_btn)
        button_layout.addSpacing(10)
        button_layout.addWidget(ascii_decrypt_btn)
        button_section.setLayout(button_layout)
        left_panel.addWidget(button_section)

        # 右侧输出区域
        output_section = QGroupBox("ASCII处理结果")
        output_layout = QVBoxLayout()

        encrypted_area = QVBoxLayout()
        encrypted_label = QLabel("加密后文本")
        encrypted_label.setStyleSheet("font-weight: bold; color: #e74c3c;")
        ascii_encrypted = QTextEdit()
        ascii_encrypted.setMaximumHeight(100)
        ascii_encrypted.setPlaceholderText("加密结果将显示在这里...")
        encrypted_area.addWidget(encrypted_label)
        encrypted_area.addWidget(ascii_encrypted)

        decrypted_area = QVBoxLayout()
        decrypted_label = QLabel("解密后文本")
        decrypted_label.setStyleSheet("font-weight: bold; color: #27ae60;")
        ascii_decrypted = QTextEdit()
        ascii_decrypted.setMaximumHeight(100)
        ascii_decrypted.setPlaceholderText("解密结果将显示在这里...")
        decrypted_area.addWidget(decrypted_label)
        decrypted_area.addWidget(ascii_decrypted)

        output_layout.addLayout(encrypted_area)
        output_layout.addSpacing(15)
        output_layout.addLayout(decrypted_area)
        output_section.setLayout(output_layout)
        right_panel.addWidget(output_section)

        layout.addLayout(left_panel)
        layout.addSpacing(20)
        layout.addLayout(right_panel)
        widget.setLayout(layout)
        return widget, ascii_input, ascii_key, ascii_encrypt_btn, ascii_decrypt_btn, ascii_encrypted, ascii_decrypted

    @staticmethod
    def create_double_tab():
        widget = QWidget()
        main_layout = QHBoxLayout()
        left_layout = QVBoxLayout()
        right_layout = QVBoxLayout()

        # 左侧输入区域
        input_panel = QGroupBox("双重加解密配置")
        input_layout = QVBoxLayout()

        plain_section = QVBoxLayout()
        plain_label = QLabel("原始数据内容")
        plain_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        double_plain = QLineEdit()
        double_plain.setPlaceholderText("输入16位数据")
        double_plain.setMinimumHeight(35)
        plain_section.addWidget(plain_label)
        plain_section.addWidget(double_plain)

        key1_section = QVBoxLayout()
        key1_label = QLabel("第一层密钥K1")
        key1_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        k1_edit = QLineEdit()
        k1_edit.setPlaceholderText("16位密钥")
        k1_edit.setMinimumHeight(35)
        key1_section.addWidget(key1_label)
        key1_section.addWidget(k1_edit)

        key2_section = QVBoxLayout()
        key2_label = QLabel("第二层密钥K2")
        key2_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        k2_edit = QLineEdit()
        k2_edit.setPlaceholderText("16位密钥")
        k2_edit.setMinimumHeight(35)
        key2_section.addWidget(key2_label)
        key2_section.addWidget(k2_edit)

        input_layout.addLayout(plain_section)
        input_layout.addSpacing(10)
        input_layout.addLayout(key1_section)
        input_layout.addSpacing(10)
        input_layout.addLayout(key2_section)
        input_panel.setLayout(input_layout)
        left_layout.addWidget(input_panel)

        # 按钮区域
        button_panel = QGroupBox("双重加解密操作")
        button_layout = QVBoxLayout()
        double_encrypt_btn = QPushButton("双重加密处理")
        double_decrypt_btn = QPushButton("双重解密处理")
        double_encrypt_btn.setMinimumHeight(40)
        double_decrypt_btn.setMinimumHeight(40)
        button_layout.addWidget(double_encrypt_btn)
        button_layout.addSpacing(10)
        button_layout.addWidget(double_decrypt_btn)
        button_panel.setLayout(button_layout)
        left_layout.addWidget(button_panel)

        # 右侧输出区域
        output_panel = QGroupBox("双重加解密结果")
        output_layout = QVBoxLayout()

        cipher_section = QVBoxLayout()
        cipher_label = QLabel("加密输出结果")
        cipher_label.setStyleSheet("font-weight: bold; color: #e74c3c;")
        double_cipher = QLineEdit()
        double_cipher.setReadOnly(True)
        double_cipher.setMinimumHeight(35)
        double_cipher.setStyleSheet("background-color: #f8f9fa;")
        cipher_section.addWidget(cipher_label)
        cipher_section.addWidget(double_cipher)

        decrypt_section = QVBoxLayout()
        decrypt_label = QLabel("解密输出结果")
        decrypt_label.setStyleSheet("font-weight: bold; color: #27ae60;")
        double_decrypt_output = QLineEdit()
        double_decrypt_output.setReadOnly(True)
        double_decrypt_output.setMinimumHeight(35)
        double_decrypt_output.setStyleSheet("background-color: #f8f9fa;")
        decrypt_section.addWidget(decrypt_label)
        decrypt_section.addWidget(double_decrypt_output)

        output_layout.addLayout(cipher_section)
        output_layout.addSpacing(15)
        output_layout.addLayout(decrypt_section)
        output_panel.setLayout(output_layout)
        right_layout.addWidget(output_panel)

        main_layout.addLayout(left_layout)
        main_layout.addSpacing(20)
        main_layout.addLayout(right_layout)
        widget.setLayout(main_layout)
        return widget, double_plain, k1_edit, k2_edit, double_encrypt_btn, double_decrypt_btn, double_cipher, double_decrypt_output

    @staticmethod
    def create_mitm_tab():
        widget = QWidget()
        layout = QVBoxLayout()

        # 输入区域
        input_panel = QGroupBox("中间相遇攻击参数")
        input_layout = QHBoxLayout()

        left_input = QVBoxLayout()
        plain_label = QLabel("已知明文数据")
        plain_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        known_plain = QLineEdit()
        known_plain.setPlaceholderText("输入已知明文")
        known_plain.setMinimumHeight(35)
        left_input.addWidget(plain_label)
        left_input.addWidget(known_plain)

        right_input = QVBoxLayout()
        cipher_label = QLabel("对应密文数据")
        cipher_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        known_cipher = QLineEdit()
        known_cipher.setPlaceholderText("输入对应密文")
        known_cipher.setMinimumHeight(35)
        right_input.addWidget(cipher_label)
        right_input.addWidget(known_cipher)

        input_layout.addLayout(left_input)
        input_layout.addSpacing(15)
        input_layout.addLayout(right_input)
        input_panel.setLayout(input_layout)
        layout.addWidget(input_panel)

        # 攻击按钮
        attack_panel = QGroupBox("攻击执行")
        attack_layout = QVBoxLayout()
        mitm_btn = QPushButton("启动中间相遇攻击")
        mitm_btn.setMinimumHeight(45)
        mitm_btn.setStyleSheet(
            "QPushButton { background-color: #e74c3c; color: white; font-weight: bold; border-radius: 5px; }")
        attack_layout.addWidget(mitm_btn)
        attack_panel.setLayout(attack_layout)
        layout.addWidget(attack_panel)

        # 结果区域
        result_panel = QGroupBox("攻击结果输出")
        result_layout = QVBoxLayout()

        result_label = QLabel("发现的密钥对组合")
        result_label.setStyleSheet("font-weight: bold; color: #8e44ad;")
        mitm_results = QTextEdit()
        mitm_results.setMaximumHeight(150)
        mitm_results.setPlaceholderText("攻击结果将显示在这里...")

        result_layout.addWidget(result_label)
        result_layout.addWidget(mitm_results)
        result_panel.setLayout(result_layout)
        layout.addWidget(result_panel)

        widget.setLayout(layout)
        return widget, known_plain, known_cipher, mitm_btn, mitm_results

    @staticmethod
    def create_triple_tab():
        widget = QWidget()
        main_layout = QHBoxLayout()
        left_layout = QVBoxLayout()
        center_layout = QVBoxLayout()
        right_layout = QVBoxLayout()

        # 左侧输入区域
        input_panel = QGroupBox("三重加解密参数")
        input_layout = QVBoxLayout()

        plain_section = QVBoxLayout()
        plain_label = QLabel("原始数据内容")
        plain_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        triple_plain = QLineEdit()
        triple_plain.setPlaceholderText("输入16位数据")
        triple_plain.setMinimumHeight(35)
        plain_section.addWidget(plain_label)
        plain_section.addWidget(triple_plain)

        input_layout.addLayout(plain_section)
        input_layout.addSpacing(15)
        input_panel.setLayout(input_layout)
        left_layout.addWidget(input_panel)

        # 中间密钥区域
        key_panel = QGroupBox("三重加密密钥")
        key_layout = QVBoxLayout()

        k1_section = QVBoxLayout()
        k1_label = QLabel("第一层密钥K1")
        k1_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        triple_k1 = QLineEdit()
        triple_k1.setPlaceholderText("16位密钥")
        triple_k1.setMinimumHeight(35)
        k1_section.addWidget(k1_label)
        k1_section.addWidget(triple_k1)

        k2_section = QVBoxLayout()
        k2_label = QLabel("第二层密钥K2")
        k2_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        triple_k2 = QLineEdit()
        triple_k2.setPlaceholderText("16位密钥")
        triple_k2.setMinimumHeight(35)
        k2_section.addWidget(k2_label)
        k2_section.addWidget(triple_k2)

        k3_section = QVBoxLayout()
        k3_label = QLabel("第三层密钥K3")
        k3_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        triple_k3 = QLineEdit()
        triple_k3.setPlaceholderText("16位密钥")
        triple_k3.setMinimumHeight(35)
        k3_section.addWidget(k3_label)
        k3_section.addWidget(triple_k3)

        key_layout.addLayout(k1_section)
        key_layout.addSpacing(10)
        key_layout.addLayout(k2_section)
        key_layout.addSpacing(10)
        key_layout.addLayout(k3_section)
        key_panel.setLayout(key_layout)
        center_layout.addWidget(key_panel)

        # 右侧输出区域
        output_panel = QGroupBox("三重加解密结果")
        output_layout = QVBoxLayout()

        # 按钮区域
        button_section = QVBoxLayout()
        triple_encrypt_btn = QPushButton("执行三重加密")
        triple_decrypt_btn = QPushButton("执行三重解密")
        triple_encrypt_btn.setMinimumHeight(35)
        triple_decrypt_btn.setMinimumHeight(35)
        button_section.addWidget(triple_encrypt_btn)
        button_section.addSpacing(10)
        button_section.addWidget(triple_decrypt_btn)

        # 结果区域
        result_section = QVBoxLayout()
        cipher_label = QLabel("加密输出")
        cipher_label.setStyleSheet("font-weight: bold; color: #e74c3c;")
        triple_cipher = QLineEdit()
        triple_cipher.setReadOnly(True)
        triple_cipher.setMinimumHeight(35)
        triple_cipher.setStyleSheet("background-color: #f8f9fa;")

        decrypt_label = QLabel("解密输出")
        decrypt_label.setStyleSheet("font-weight: bold; color: #27ae60;")
        triple_decrypt_output = QLineEdit()
        triple_decrypt_output.setReadOnly(True)
        triple_decrypt_output.setMinimumHeight(35)
        triple_decrypt_output.setStyleSheet("background-color: #f8f9fa;")

        result_section.addWidget(cipher_label)
        result_section.addWidget(triple_cipher)
        result_section.addSpacing(15)
        result_section.addWidget(decrypt_label)
        result_section.addWidget(triple_decrypt_output)

        output_layout.addLayout(button_section)
        output_layout.addSpacing(20)
        output_layout.addLayout(result_section)
        output_panel.setLayout(output_layout)
        right_layout.addWidget(output_panel)

        main_layout.addLayout(left_layout)
        main_layout.addSpacing(15)
        main_layout.addLayout(center_layout)
        main_layout.addSpacing(15)
        main_layout.addLayout(right_layout)
        widget.setLayout(main_layout)
        return widget, triple_plain, triple_k1, triple_k2, triple_k3, triple_encrypt_btn, triple_decrypt_btn, triple_cipher, triple_decrypt_output

    @staticmethod
    def create_cbc_tab():
        widget = QWidget()
        layout = QVBoxLayout()

        # 顶部输入区域
        input_panel = QGroupBox("CBC模式配置")
        input_layout = QHBoxLayout()

        left_input = QVBoxLayout()
        plain_label = QLabel("原始文本内容")
        plain_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        cbc_plain = QTextEdit()
        cbc_plain.setMaximumHeight(80)
        cbc_plain.setPlaceholderText("输入待处理文本...")
        left_input.addWidget(plain_label)
        left_input.addWidget(cbc_plain)

        center_input = QVBoxLayout()
        key_label = QLabel("加密密钥设置")
        key_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        cbc_key = QLineEdit()
        cbc_key.setPlaceholderText("16位密钥")
        cbc_key.setMinimumHeight(35)
        center_input.addWidget(key_label)
        center_input.addWidget(cbc_key)

        right_input = QVBoxLayout()
        iv_label = QLabel("初始化向量IV")
        iv_label.setStyleSheet("font-weight: bold; color: #2c3e50;")
        cbc_iv = QLineEdit()
        cbc_iv.setPlaceholderText("16位初始向量")
        cbc_iv.setMinimumHeight(35)
        right_input.addWidget(iv_label)
        right_input.addWidget(cbc_iv)

        input_layout.addLayout(left_input)
        input_layout.addSpacing(15)
        input_layout.addLayout(center_input)
        input_layout.addSpacing(15)
        input_layout.addLayout(right_input)
        input_panel.setLayout(input_layout)
        layout.addWidget(input_panel)

        # 按钮区域
        button_panel = QGroupBox("CBC模式操作")
        button_layout = QHBoxLayout()
        cbc_encrypt_btn = QPushButton("CBC加密处理")
        cbc_decrypt_btn = QPushButton("CBC解密处理")
        tamper_btn = QPushButton("密文篡改测试")
        cbc_encrypt_btn.setMinimumHeight(40)
        cbc_decrypt_btn.setMinimumHeight(40)
        tamper_btn.setMinimumHeight(40)
        button_layout.addWidget(cbc_encrypt_btn)
        button_layout.addWidget(cbc_decrypt_btn)
        button_layout.addWidget(tamper_btn)
        button_panel.setLayout(button_layout)
        layout.addWidget(button_panel)

        # 输出区域
        output_panel = QGroupBox("CBC处理结果")
        output_layout = QHBoxLayout()

        left_output = QVBoxLayout()
        encrypt_label = QLabel("加密结果输出")
        encrypt_label.setStyleSheet("font-weight: bold; color: #e74c3c;")
        cbc_encrypted = QTextEdit()
        cbc_encrypted.setMaximumHeight(80)
        cbc_encrypted.setPlaceholderText("加密结果...")
        left_output.addWidget(encrypt_label)
        left_output.addWidget(cbc_encrypted)

        center_output = QVBoxLayout()
        decrypt_label = QLabel("解密结果输出")
        decrypt_label.setStyleSheet("font-weight: bold; color: #27ae60;")
        cbc_decrypted = QTextEdit()
        cbc_decrypted.setMaximumHeight(80)
        cbc_decrypted.setPlaceholderText("解密结果...")
        center_output.addWidget(decrypt_label)
        center_output.addWidget(cbc_decrypted)

        right_output = QVBoxLayout()
        tamper_label = QLabel("篡改测试结果")
        tamper_label.setStyleSheet("font-weight: bold; color: #f39c12;")
        tamper_result = QTextEdit()
        tamper_result.setMaximumHeight(80)
        tamper_result.setPlaceholderText("篡改测试结果...")
        right_output.addWidget(tamper_label)
        right_output.addWidget(tamper_result)

        output_layout.addLayout(left_output)
        output_layout.addSpacing(15)
        output_layout.addLayout(center_output)
        output_layout.addSpacing(15)
        output_layout.addLayout(right_output)
        output_panel.setLayout(output_layout)
        layout.addWidget(output_panel)

        widget.setLayout(layout)
        return widget, cbc_plain, cbc_key, cbc_iv, cbc_encrypt_btn, cbc_decrypt_btn, tamper_btn, cbc_encrypted, cbc_decrypted, tamper_result


